--[[
	Copyright (c) 2012 Eyal Shilony

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local addonName, addon = ...
local options = addon:NewModule("Options")
local config = LibStub("AceConfigRegistrar-1.0")
local shortcuts = LibStub("LibShortcuts-4.0")
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

local Buttons = {
	KEY_BUTTON1,
	KEY_BUTTON2,
}

local Modifier = {
	ALT_KEY,
	CTRL_KEY,
	SHIFT_KEY,
	NONE
}

options:RegisterEvent("OnEnabled", function(self, even)
	local options = {
		name = addon.name,
		type = "group",
		args = {
			{
				type = "description",
				name = function()
					return GetAddOnMetadata(addonName, "Notes")
				end,
				cmdHidden = true,
			},
			{
				type = "header",
				name = "",
			},
			{
				type = "description",
				name = "",
			},
			{
				type = "toggle",
				name = L["Blocks the menu while in combat."],
				get = function()
					return addon.db.profile.blockInCombat
				end,
				set = function()
					addon.db.profile.blockInCombat = not addon.db.profile.blockInCombat
				end,
				width = "full",
			},
			{
				type = "header",
				name = L["Broker Display Pattern"],
			},
			{
				type = "description",
				name = L["You can customize the display to your liking using plain text and the tags below."],
			},
			{
				type = "description",
				name = "",

			},
			{
				type = "description",
				name = L["@latency: the latency.\n"]..
						L["@fps: the framerate per second.\n"]..
						L["@memory: the total memory addons are taking.\n"]..
						L["@bags: the total free space of your bags."]..
						"\n",
			},
			{
				type = "input",
				name = "",
				get = function()
					return addon.db.profile.display
				end,
				set = function(_, value)
					addon.db.profile.display = value
				end,
				width = "full",
			},
			{
				type = "description",
				name = "",
			},
			{
				type = "header",
				name = L["Performance Tooltip"],
			},
			{
				type = "description",
				name = "",
			},
			{
				type = "toggle",
				name = L["Show Performance"],
				get = function()
					return addon.db.profile.showPerformance
				end,
				set = function()
					addon.db.profile.showPerformance = not addon.db.profile.showPerformance
				end,
				width = "full",
			},
			{
				type = "range",
				name = L["Number of addons to display"],
				get = function()
					return addon.db.profile.numAddons
				end,
				set = function(_, value)
					addon.db.profile.numAddons = value
				end,
				step = 1,
				min = 1,
				max = 40,
				width = "full",
			},
		},
	}
	config:RegisterOptions(options)
	
	local accessibility = {
		name = L["Accessibility"],
		type = "group",
		args = {
			{
				type = "description",
				name = L["Set the shortcuts accessibility."],
				cmdHidden = true,
			},
			{
				type = "header",
				name = "",
			},
			{
				type = "select",
				name = L["Mouse Setting"],
				get = function()
					return addon.db.profile.mouseButton
				end,
				set = function(_, value)
					addon.db.profile.mouseButton = value
				end,
				values = Buttons,
			},
			{
				type = "header",
				name = "",
			},
		},
	}
	
	for i, v in pairs(addon.db.profile.accessibility) do
		table.insert(accessibility.args, {
			type = "select",
			name = L["Shortcut %s"]:format(i),
			get = function()
				local _, index = shortcuts:GetShortcut(addon.db.profile.accessibility[i].shortcut)
				return index
			end,
			set = function(_, value)
				local shortcut = shortcuts:GetShortcutByIndex(value)
				addon.db.profile.accessibility[i].shortcut = shortcut.name
			end,
			values = shortcuts:GetShortcutsTitles(),
			width = "double",
		})
		table.insert(accessibility.args, {
			type = "select",
			name = L["Modifier"],
			get = function()
				return addon.db.profile.accessibility[i].modifier
			end,
			set = function(_, value)
				addon.db.profile.accessibility[i].modifier = value
			end,
			values = Modifier,
		})
	end
	config:RegisterOptions(accessibility)
	
	local priorities = {
		name = L["Priorities"],
		type = "group",
		args = {
			{
				type = "description",
				name = L["Manage priorities.\n\nNote, having no priority (zero) hides the item."],
				cmdHidden = true,
			},
			{
				type = "header",
				name = "",
			},
		},
	}
	
	local count = shortcuts:NumOfShortcuts()
	for _, v in shortcuts:IterateShortcuts() do
		table.insert(priorities.args, {
			type = "range",
			name = v.title,
			get = function()
				return addon.db.profile.shortcuts[v.name]
			end,
			set = function(_, value)
				addon:UpdateMenu(v.name, value)
			end,
			step = 1,
			min = 0,
			max = count,
			width = "full",
		})
	end
	config:RegisterOptions(priorities)
	
	config:RegisterOptions(LibStub("AceDBOptions-3.0"):GetOptionsTable(addon.db), true)
end)
